package controllers;

import java.util.List;

import models.Employee;
import play.mvc.Controller;
import dao.EmployeeDao;

public class Employees extends Controller {

	public static void index() {
		EmployeeDao employeeDao = new EmployeeDao();
		List<Employee> entities = employeeDao.findAll();
		render(entities);
	}

	public static void create(Employee employee) {
		render(employee);
	}

	public static void save(Employee employee) {
		EmployeeDao employeeDao = new EmployeeDao();
		if(employee.getId() != null) {
			Employee oldEmployee = employeeDao.findById(employee.getId());
			if(oldEmployee != null && oldEmployee.getDepartment()!= null) {
				employee.setDepartment(oldEmployee.getDepartment());
			}
		}
		employeeDao.save(employee);
		index();
	}

	public static void edit(Long id) {
		EmployeeDao employeeDao = new EmployeeDao();
		Employee employee = employeeDao.findById(id);
		render("Employees/create.html", employee);
	}

	public static void delete(Long id) {
		EmployeeDao employeeDao = new EmployeeDao();
		employeeDao.delete(id);
		index();
	}
	
	public static void addEmployeesToDepartment(Long id) {
		EmployeeDao employeeDao = new EmployeeDao();
		employeeDao.addEmployeesToDepartment(id);
		index();
	}

}
